<?php
class PagesController extends AppController {
	var $uses = array();

	function beforeFilter() {
		parent::beforeFilter();
		$this->CustomAuth->allow('*');
	}

	function index() {
		$this->set(array(
			'title_for_layout' => 'Adams Campus'
		));
		$this->set(array(
			'hideBanner' => false,
		));
	}

	function display($page) {
		switch ($page) {
			case 'concept':
				$titleForLayout = 'コンセプト - Adams Campus';
				break;
			case 'tokutei':
				$titleForLayout = '特定商取引法に基づく表示 - Adams Campus';
				break;
			case 'faq':
				$titleForLayout = 'サイトの使い方 - Adams Campus';
				break;
			case 'terms':
				$titleForLayout = '利用規約 - Adams Campus';
				break;
			case 'privacy':
				$titleForLayout = 'プライバシーポリシー - Adams Campus';
				break;
			default:
				$this->cakeError('error404');
		}
		$this->set(array(
			'hideBanner' => false,
			'title_for_layout' => $titleForLayout,
		));
		return $this->render($page);
	}

	function contact() {
		if ($this->RequestHandler->isPost() && $this->data) {
			$this->loadModel('Contact');
			if ($this->Contact->save($this->data)) {
				$this->Session->setFlash(Messages::thank_for_your_contact_we_will_reply_to_you_soon, 'flash_message', array(), 'success');
				$this->_sendContactEmail();
				$this->redirect(array('action' => $this->action));
			}
		}
		$this->loadModel('ContactType');
		$this->set(array(
			'title_for_layout' => 'Contact Us - Adams Campus',
			'types' => $this->ContactType->find('list'),
		));
	}

	function _sendContactEmail() {
		$this->loadModel('ContactType');
		$contactType = $this->ContactType->read('name', $this->data['Contact']['type']);
		$type = '';
		if (isset($contactType['ContactType']['name'])) {
			$type = $contactType['ContactType']['name'];
		}
		$this->_initEmail();
		//send to admin
		$this->Email->to = Configure::read('Notification.site_email');
		$this->Email->subject = Messages::email_subject_contact_admin;
		$this->Email->template = 'contact_admin';
		$this->set(array(
			'contact' => $this->data,
			'contactType' => $type,
		));
		$this->Email->send();

		//send to user
		$this->Email->to = $this->data['Contact']['email'];
		$this->Email->subject = Messages::email_subject_contact_user;
		$this->Email->template = 'contact_user';
		$this->set(array(
			'contact' => $this->data,
			'contactType' => $type,
		));
		$this->Email->send();
	}

	function admin_export($type = null) {
		switch ($type) {
			case 'profile':
				$this->_exportProfile();
				break;
			case 'bank':
				$this->_exportBank();
				break;
			default:
				$this->cakeError('error404');
		}
		$this->header('Content-Type:text/csv;charset=shift_jis');
		$this->header('Content-Disposition:attachment;filename=' . $type . '.csv');
		$out = fopen('php://temp', 'r+');
		fputcsv($out, $this->header);
		foreach ($this->data as $r) {
			fputcsv($out, $r);
		}
		rewind($out);
		$csv = '';
		while ($line = fgets($out)) {
			$csv .= $line;
		}
		echo mb_convert_encoding($csv, 'shift-jis', 'utf-8');
		$this->layout = false;
		return $this->render(false);

	}

	function _exportProfile() {
		$this->loadModel('User');
		$this->header = array(
			'ユーザID', //id
			'メールアドレス', //email
			'名', //first_name
			'姓', //last_name
			'肩書き', //headline
			'プロフィール詳細', //description
			'メールマガジン配信フラグ', //mailmagazine
			'メールウィッシュリスト', //mail_whislist_flg
			'プロフィール公開フラグ', //profile_setting
			'現在のポイント', //now_point
			'最大ポイント', //max_point
			'ユーザデータ登録日時', //created
			'権限', //role
			'ユーザ削除フラグ', //del_flg
		);
		$this->data = $this->User->getProfileExport();

	}

	function _exportBank() {
		$this->loadModel('BankInformation');
		$this->header = array(
			"バンクID", //id (bank)
			"ユーザID", // user_id
			"名", // first_name
			"姓", // last_name
			"銀行名", // bank_name
			"支店名", // branch_name
			"口座種類", // type
			"口座番号", // account_num
			"振込名義", // payee_name
			"ユーザデータ登録日時", // created
			"権限", // role
			"ユーザ削除フラグ" // del_flg
		);
		$this->data = $this->BankInformation->getBankInfoCsvExport();
	}
}